package com.woyowodaows.utils;

import android.app.Activity;


import com.woyowodaows.activity.home.LoginActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * @author haiwei
 *         Activity栈管理
 *         2017/2/22
 */
public class ActivityManagerUtils {
    private static List<Activity> activityStack;
    private static ActivityManagerUtils instance;

    private ActivityManagerUtils() {

    }

    public static ActivityManagerUtils getInstance() {
        if (instance == null) {
            synchronized (ActivityManagerUtils.class) {
                if (instance == null) {
                    instance = new ActivityManagerUtils();
                }
            }
        }
        return instance;
    }

    /**
     * @param activity 添加Activity到堆栈
     */
    public void addActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new ArrayList<>();
        }
        if (!activityStack.contains(activity)) {
            activityStack.add(activity);
        }
    }

    /**
     * 删除堆栈中的当前Activity
     */
    public void removeActivity(Activity activity) {
        if (activity != null) {
            if (activityStack.contains(activity)) {
                activityStack.remove(activity);
            }
        }
    }


    /**
     * 关闭指定Activity
     *
     * @param activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null && !activity.isFinishing()) {
            activity.finish();
            if (!stackIsNullOrEmppty()) {
                activityStack.remove(activity);
                activity.finish();
            }
        }

    }

    /**
     * 根据Class关闭Activity
     *
     * @param cls
     */
    public void finishActivity(Class<?> cls) {
        if (cls == null || stackIsNullOrEmppty())
            return;
        for (Activity activity : activityStack) {
            if (activity.getClass().equals(cls)) {
                finishActivity(activity);
                break;
            }
        }
    }

    /**
     * 关闭所有Activity
     */
    public void finishAllActivity() {
        if (stackIsNullOrEmppty())
            return;
        for (int i = 0, size = activityStack.size(); i < size; i++) {
            if (null != activityStack.get(i) && !activityStack.get(i).isFinishing()
                    && !isMainActivity(activityStack.get(i))) {
                activityStack.get(i).finish();
            }
        }
        activityStack.clear();
    }

    /**
     * 是否是主界面
     */
    private boolean isMainActivity(Activity activity) {
        return activity instanceof LoginActivity;
    }


    public Activity getActivity(Class<?> cls) {
        if (stackIsNullOrEmppty())
            return null;
        for (Activity activity : activityStack) {
            if (activity.getClass().equals(cls))
                return activity;

        }
        return null;
    }

    /**
     * 判断堆栈是否为空
     *
     * @return
     */
    public boolean stackIsNullOrEmppty() {
        if (activityStack == null || activityStack.size() == 0)
            return true;
        return false;
    }

    /**
     * 清空所有activity并退出
     *
     * @param cls
     */
    public void closeAllExceptThis(Class<?> cls) {
        // Activity act=getActivity(cls);
        for (Activity a : activityStack) {
            if (a.getClass().equals(cls)) {
                continue;
            } else {
                finishActivity(a);
            }
        }
    }
}
